Java-如何创建二叉查找树

原文来自:https://goo.gl/EieLZK

简介

这篇文章主要讲如何创建二叉查找树。如果有哪些不对的地方请指正,欢迎批评和建议。

要点

本文将依照一下的几点介绍

  1. 什么是二叉查找数
  2. 二叉查找树的遍历
  3. 代码例子

二叉查找树的定义

一个二叉树如果想成为二叉查找树需要满足以下条件:
(1)若任意节点的左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
(2)若任意节点的右子树不为空,则右子树上所有节点的值均大于或等于根节点的值;
(3)任意节点的左、右子树也分别为二叉查找树。

下图所示为一棵二叉查找树
这里写图片描述

二叉查找树的遍历

二叉查找树的遍历和普通的二叉树遍历没有本质上的区别。

(1)先序遍历:按照 根-左子树-右子树 的顺序遍历;
(2)中序遍历:按照 左子树-根-右子树 的顺序遍历;
(3)后序遍历:按照 左子树-右子树-根 的顺序遍历

代码例子

给出数组:{20, 15, 200, 25, -5, 0, 100, 20, 12, 126, 1000, -150} 作为节点上的值,如何创建以及遍历二叉查找数呢?我们此处假设根节点的值为20(可以选择任意一个值作为根节点值)。

public class TreeNode {
    int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int x) {
        val = x;
    }

    public void addNode(int num) {
        if (num < this.val) {
            if (this.left != null) {
                this.left.addNode(num);
            } else {
                this.left = new TreeNode(num);
            }
            return;
        }

        if (this.right != null) {
            this.right.addNode(num);
        } else {
            this.right = new TreeNode(num);
        }
        return;
    }
    // Visit the node first, then left and right sub-trees
    public void traversePreOrder() {
        System.out.println(this.val);
        if (this.left != null) {
            this.left.traversePreOrder();
        }
        if (this.right != null) {
            this.right.traversePreOrder();
        }
    }

    // Visit left sub-tree, then node and then, right sub-tree
    public void traverseInOrder() {
        if (this.left != null) {
            this.left.traverseInOrder();
        }
        System.out.println(this.val);
        if (this.right != null) {
            this.right.traverseInOrder();
        }
    }

    // Visit left sub-tree, then right sub-tree and then the node
    public void traversePostOrder() {
        if (this.left != null) {
            this.left.traversePostOrder();
        }
        if (this.right != null) {
            this.right.traversePostOrder();
        }
        System.out.println(this.val);
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
查找(Binary Search Tree)是一种常见的数据结构,它的每个节点最多有两个子节点,左子节点的值小于父节点的值,右子节点的值大于父节点的值。Java 实现二查找的代码如下: ``` public class BinarySearchTree { private Node root; private class Node { private int key; private Node left; private Node right; public Node(int key) { this.key = key; } } public void insert(int key) { root = insert(root, key); } private Node insert(Node x, int key) { if (x == null) { return new Node(key); } if (key < x.key) { x.left = insert(x.left, key); } else if (key > x.key) { x.right = insert(x.right, key); } return x; } public boolean contains(int key) { return contains(root, key); } private boolean contains(Node x, int key) { if (x == null) { return false; } if (key == x.key) { return true; } else if (key < x.key) { return contains(x.left, key); } else { return contains(x.right, key); } } public void delete(int key) { root = delete(root, key); } private Node delete(Node x, int key) { if (x == null) { return null; } if (key < x.key) { x.left = delete(x.left, key); } else if (key > x.key) { x.right = delete(x.right, key); } else { if (x.left == null) { return x.right; } else if (x.right == null) { return x.left; } else { Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; } } return x; } private Node min(Node x) { if (x.left == null) { return x; } else { return min(x.left); } } private Node deleteMin(Node x) { if (x.left == null) { return x.right; } x.left = deleteMin(x.left); return x; } } ``` 这段代码实现了二查找的插入、查找和删除操作。其中,插入操作使用递归实现,查找和删除操作也是递归实现的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值